<template>
	<view>
		<image src="/static/time_line/user_top.png" mode="widthFix" class="user_top_bg"></image>
		<view class="container">
			<view class="user_info_box">
				<image :src="user_info.avatar" class="user_head" mode="aspectFill"></image>
				<view class="user_info">
					<view class="user_name">
						{{user_info.nickname}}
					</view>
					<view class="fans_num">
						{{fans_count}}粉丝
					</view>
				</view>
				<view class="guanzhu_btn" :class="{guanzhu_on:is_guanzhu}" v-if="!is_myself" @tap="go_guanzhu">
					{{is_guanzhu?'已关注':'关注'}}
				</view>
			</view>
			<image src="/static/time_line/dongtai.png" mode="widthFix" class="dongtai"></image>

			<timeline v-for="item,id in list" @longPressDelete="delete_timeline" @go_detail="go_detail"
				:show_report_box="!is_myself" :is_navto="false" :show_attention="false" :time_line_data_="item">
			</timeline>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				user_info: {},
				fans_count: 0,
				is_guanzhu: 0,
				is_myself: 0,


				list: [],
				page: 1,
				limit: 10,
				hasNextPage: true
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.get_data_list()
		},
		onPullDownRefresh() {
			this.get_data_list();
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 2000)
		},
		onReachBottom() {
			if (this.hasNextPage) {
				let new_page = this.page + 1;
				let data = {
					page: new_page,
					limit: this.limit,
				}
				let token = (this.$db.get('userToken'));
				if (token) data.token = token
				this.$api.getUserList(data, res => {
					if (res.status) {
						this.list = this.list.concat(res.data);
						this.hasNextPage = res.data.length >= this.limit;
						this.page = new_page
					}
				})
			}
		},
		methods: {
			go_detail: function(id) {
				uni.navigateTo({
					url: '/pages/timeline/details?id=' + id
				})
			},
			delete_timeline: function(id) {
				console.log(this.is_myself)
				if (this.is_myself) {
					uni.showModal({
						content: '确定删除动态吗？',
						success: (res) => {
							if (res.confirm) {
								let data = {
									id: id
								}
								this.$api.deleteTimeline(data, res => {
									if (res.status) {
										this.$common.successToShow(res.msg)
										uni.navigateBack()
									} else {
										this.$common.errorToShow(res.msg)
									}
								})
							}
						}
					});

				}
			},

			go_guanzhu: function() {
				let focus = () => {
					let data = {
						user_id: this.id
					}
					this.$api.addFocus(data, res => {
						if (res.status) {
							this.is_guanzhu = res.data
						} else {
							this.$common.errorToShow(res.msg)
						}
					})
				}
				if (this.is_guanzhu) {
					uni.showModal({
						content: '确定取消关注吗?',
						success: (res) => {
							if (res.confirm) {
								focus()
							}
						}
					});
				} else {
					focus()
				}
			},

			get_data_list: function() {
				let data = {
					user_id: this.id,
					page: this.page,
					limit: this.limit
				}
				let token = (this.$db.get('userToken'));
				if (token) data.token = token

				this.$api.getUserList(data, res => {
					if (res.status) {
						this.list = res.data;
						this.user_info = res.user;
						this.fans_count = res.fans_count
						this.is_guanzhu = res.is_guanzhu
						this.is_myself = res.is_myself
					}
				})
			}
		}
	}
</script>

<style>
	.dongtai {
		margin-top: 40rpx;
		width: 59rpx;
		height: 42rpx;
		margin-bottom: 30rpx;
	}

	.guanzhu_btn {
		width: 117rpx;
		height: 42rpx;
		border-radius: 30rpx;
		border: 1rpx solid #FFE4B4;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 27rpx;
		color: #FFE4B4;
	}

	.guanzhu_on {
		background-color: #FFE4B4;
		color: #101010;
		border: none;
	}

	.fans_num {
		line-height: 40rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #989898;
	}

	.user_name {
		line-height: 40rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: #FFFFFF;
	}

	.user_info {
		padding-left: 15rpx;
		flex: 1;
	}

	.user_head {
		width: 95rpx;
		height: 95rpx;
		border-radius: 50%;

	}

	.user_info_box {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.container {
		width: 100%;
		padding: 200rpx 45rpx;
		padding-bottom: 30rpx;
	}

	.user_top_bg {
		width: 100%;
		height: 307rpx;
		position: absolute;
		top: 0;
		left: 0;
	}

	page {
		background-color: #0f0f0f;
	}
</style>
